<template>
  <el-breadcrumb
    separator="/"
    class="mb-1"
    :class="{ 'mx-1': useLocale.isMob }"
    style="vertical-align: bottom"
  >
    <el-breadcrumb-item>
      <div class="flex align-center">
        <el-icon :size="18"> <Monitor /> </el-icon
        ><span class="pl">{{ $t("menu.promotion") }}</span>
      </div>
    </el-breadcrumb-item>
    <el-breadcrumb-item>{{ $t("menu.promotion-act") }}</el-breadcrumb-item>
  </el-breadcrumb>
  <div v-if="useLocale.isMob">
    <div class="mx-15 my-15 flex justify-end">
      <el-button
        :icon="Search"
        size="small"
        circle
        @click="
          () => {
            rel.mobFilter = true;
          }
        "
      />
    </div>

    <div class="mob-list mt-2">
      <el-scrollbar ref="scrollbarRef" class="mob-scroll">
        <div
          class="mob-list-item text-content-color font"
          v-for="(item, index) in tableData"
        >
          <div class="flex justify-between border-bottom py-15">
            <div class="text-ellipsis font-mder text-main-color" style="width: 80%">
              {{ item.act_name }}
            </div>
            <div class="font" :class="{ 'text-danger': item.status == 1 }">
              {{ item.status_name }}
            </div>
          </div>

          <div class="flex justify-between border-bottom py-15">
            <span>{{$t("promotion.budget") }}({{rel.currency}})</span>
              <span>{{ item.budget }}</span>         
          </div>
          <div class="flex justify-between border-bottom py-15">
            <span>{{ $t("promotion.time") }}</span>
              <span>{{ item.time }}</span>  
          </div>
          <div class="flex justify-between py-15">
            <span>{{ $t("promotion.duration") }}</span>
            <span>{{ item.duration }}</span>
          </div>
          <div wrap class="flex flex-wrap justify-end my-1">
            <el-button
              class="mb-1"
              size="small"
              plain
              round
              @click="handleReport(index, item)"
              >{{ $t("promotion.report") }}</el-button
            >
  
 
            <el-button
              class="mb-1"
              size="small"
              plain
              round
              type="primary"
              @click="handleEdit(index, item)"
              >{{ $t("common.edit") }}</el-button
            >
          </div>
        </div>
      </el-scrollbar>
    </div>

    <div v-if="rel.total > params.page_size" class="my-1 flex justify-center">
      <el-pagination
        v-model:current-page="params.current_page"
        v-model:page-size="params.page_size"
        :pager-count="5"
        :small="false"
        :disabled="false"
        :background="true"
        layout="prev, pager, next"
        :total="rel.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
  <el-card v-else class="box-card" shadow="hover">
    <template #header>
      <div class="flex justify-between flex-wrap">
        <el-space wrap :size="10">
          <el-input
            v-model="params.act_name"
            style="width: 150px"
            :placeholder="$t('promotion.actname')"
          />
          <el-select
            v-model="params.status"
            clearable
            :placeholder="$t('promotion.actstatus')"
            style="width: 150px"
          >
            <el-option
              v-for="item in rel.status"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
          <el-button :icon="Search" plain @click="search">{{
            $t("common.query")
          }}</el-button>
        </el-space>

        <el-button type="primary" plain @click="handleCreate">新增活動</el-button>

      </div>
    </template>

    <el-table ref="multipleTableRef" :data="tableData" style="width: 100%">
      <el-table-column prop="act_name" :label="$t('promotion.actname')" />
      <el-table-column prop="budget" :label="$t('promotion.budget')+'('+rel.currency+')'" />
      <el-table-column prop="time" :label="$t('promotion.time')" />
      <el-table-column prop="duration" :label="$t('promotion.duration')" />
      <el-table-column prop="status_name" :label="$t('promotion.status')" />
      <el-table-column
        fixed="right"
        :label="$t('common.operation')"
        width="150"
      >
        <template #default="scope">
          <div class="ec2-btncon">
            <el-button
              link
              size="small"
              type="primary"
              @click="handleEdit(scope.$index, scope.row)"
              >{{ $t("common.edit") }}</el-button
            >
            <el-button
              link
              size="small"
              type="danger"
              @click="handleReport(scope.$index, scope.row)"
              >{{ $t("promotion.report") }}</el-button
            >
          </div>
        </template>
      </el-table-column>
    </el-table>
    <div v-if="rel.total > params.page_size" class="mt-2 flex justify-center">
      <el-pagination
        v-model:current-page="params.current_page"
        v-model:page-size="params.page_size"
        :page-sizes="[20, 40, 60]"
        :small="false"
        :disabled="false"
        :background="true"
        layout="total, sizes, prev, pager, next"
        :total="rel.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </el-card>

  <el-drawer v-model="rel.mobFilter" size="80%">
    <template #header>
      <h4>{{ $t("common.filter") }}</h4>
    </template>
    <template #default>
      <el-space direction="vertical" fill style="width: 100%">
        <div class="mb-1">
          <div class="font-sm text-tips-color mb">
            {{ $t("promotion.actname") }}
          </div>
          <el-input v-model="params.act_name" />
        </div>
        <div class="mb-1">
          <div class="font-sm text-tips-color mb">
            {{ $t("promotion.actstatus") }}
          </div>
          <el-select v-model="params.status" style="width: 100%">
            <el-option
              v-for="item in rel.status"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </div>
      </el-space>
    </template>
    <template #footer>
      <div style="flex: auto">
        <el-button @click="mobClearFilter">{{ $t("common.clear") }}</el-button>
        <el-button type="primary" @click="search">{{
          $t("common.query")
        }}</el-button>
      </div>
    </template>
  </el-drawer>
</template>
<script lang="ts" setup>
import { nextTick, ref, onMounted, reactive, watch, computed } from "vue";
import { Search, Monitor, ArrowDown } from "@element-plus/icons-vue";
import { $activity, $getStatus } from "@api/promotion";
import { useLocaleStore } from "@/store/locales";
import { useI18n } from "vue-i18n";
import { useRouter } from "vue-router";
const router = useRouter();
const { t } = useI18n();
const langkey = computed(() => useLocale.locale);
const useLocale = useLocaleStore();
const scrollbarRef = ref();
const rel = reactive({
  mobFilter: false,
  status:[],
  total:0,
  currency:useLocale.dictionary.currency,//币种
});
let params = ref({
  act_name: "",
  status: "",
  current_page: 1,
  page_size: 10,
});
const tableData = ref([]);
onMounted(() => {
  getFilter();
});
const getFilter = async () => {
  const status_res = await $getStatus();
  rel.status = status_res.data.list;
  getData();
};

const getData = async () => {
  let res = await $activity(params.value);
  if (res.code == 0) {
    tableData.value = res.data.table;
    rel.total = res.data.total;
    if (useLocale.isMob) {
      nextTick(() => {
        scrollbarRef.value.setScrollTop(0);
      });
    }
  }
};
const handleSizeChange = (val: number) => {
  params.value.page_size = val;
  getData();
};
const handleCurrentChange = (val: number) => {
  params.value.current_page = val;
  getData();
};

const search = () => {
  console.log(params.value);
  getData();
  rel.mobFilter = false;
};

const handleCreate = (index, row) => {
  console.log(row);
  router.push({
    path: "/promotion/createAct",
  });
};
const handleEdit = (index, row) => {
  console.log(row);
  router.push({
    path: "/promotion/editAct",
    query: {
      id: row.act_id
    },
  });
};
const handleReport = (index, row) => {
  console.log(row);
  // router.push({
  //   path: "/user/info",
  //   query: {
  //     id: row.euid,
  //   },
  // });
};




const mobClearFilter = () => {
  params.value.act_name = "";
  params.value.status = "";
  params.value.current_page = 1;
  params.value.page_size = 10;
  getData();
};

watch(langkey, (newVal, oldVal) => {
  console.log("新值:", newVal, "老值:", oldVal);
  getFilter();
});
</script>
<style>
.ec2-search {
  margin-right: 5px;
  .el-input__wrapper {
    border-radius: 30px;
  }
}
.ec2-btncon {
  .el-button {
    margin-bottom: 2px;
    justify-content: flex-start;
  }
  .el-button + .el-button {
    margin-left: 0;
  }
}
</style>
<style lang="scss" scoped>
.mob-scroll {
  height: calc(100vh - 300px);
}
.mob-list {
  .mob-list-item {
    background: #fff;
    border-radius: 8px;
    padding: 15px;
    margin-bottom: 10px;
  }
  .desc-item {
    padding: 15px 0;
    border-bottom: 1px solid #ebeef5;
  }
}
.consume {
  padding-left: 2px;
  color: var(--el-color-primary);
}
.ec2-btncon {
  display: flex;
  flex-direction: column;
}
</style>
